/**
 * 用户虚拟币
 */
import Vue from 'vue'
const SUCCESS = 1000000
const API_ROOT = server.root + '/newmanage'

const searchUserResource = Vue.resource(API_ROOT + '/gold/list')
const searchTotalResource = Vue.resource(API_ROOT + '/gold/total')
const searchRecordResource = Vue.resource(API_ROOT + '/gold/stream')
const modifyRecordResource = Vue.resource(API_ROOT + '/gold/stream/record')
const batchupdateResource = Vue.resource(API_ROOT + '/gold/batchupdate')
const statisticsResource = Vue.resource(API_ROOT + '/gold/use/statistics')

export const searchUser = ({dispatch}, params) => {
  return searchUserResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('UPDATE_MONEY_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const searchTotal = ({dispatch}, params) => {
  return searchTotalResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data.totalBalance)
      }
      return Promise.reject(res.data)
    })
}
export const searchRecord = ({dispatch}, params) => {
  return searchRecordResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const modifyRecord = ({dispatch}, params) => {
  return modifyRecordResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const useStatistics = ({dispatch}, params) => {
  return statisticsResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const renderUserlist = ({dispatch}, params) => {
  return dispatch('RENDER_USER_list', params)
}
export const renderStreamlist = ({dispatch}, params) => {
  return dispatch('RENDER_STREAM_list', params)
}
export const updateMoney = ({dispatch}, params) => {
  return batchupdateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('UPDATE_MONEY_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const backtToUserList = ({dispatch}, params) => {
  return dispatch('BACK_TO_LIST', params)
}
